<?php

include_once 'Classes/PHPCodeProvider.php';

/*
 * Create an instance of the CodeGeneratorOptions class
 * using the default options for code generation.
 */

$options = new CodeGeneratorOptions();

/*
 * Create an instance of the IndentedTextWriter.
 * After the generateCodeFromStatement call
 * $writer will hold the generated code.
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the PHPCodeProvider class
 */

$provider = new PHPCodeProvider();

/*
 * $i will be used as the condition expression for the switch statements
 */

$i = new VariableReferenceExpression('i');

/*
 * The first switch statement
 */

$switchStatement1 = new SwitchStatement($i);

$case1 = new CaseStatement(new PrimitiveExpression(0));
$case1->Statements->add(new SnippetStatement('        echo "i equals 0"'));

$switchStatement1->CaseStatements->add($case1);

$case2 = new CaseStatement(new PrimitiveExpression(1));
$case2->Statements->add(new SnippetStatement('        echo "i equals 1"'));

$switchStatement1->CaseStatements->add($case2);

$case3 = new CaseStatement(new PrimitiveExpression(2));
$case3->Statements->add(new SnippetStatement('        echo "i equals 2"'));

$switchStatement1->CaseStatements->add($case3);

/*
 * Generate the code and output the result.
 */

$provider->generateCodeFromStatement($switchStatement1, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

$writer->clear();

/*
 * The second switch statement
 */

$switchStatement2 = new SwitchStatement($i);

$case4 = new CaseStatement(new PrimitiveExpression('apple'));
$case4->Statements->add(new SnippetStatement('        echo "i is apple"'));

$switchStatement2->CaseStatements->add($case4);

$case5 = new CaseStatement(new PrimitiveExpression('bar'));
$case5->Statements->add(new SnippetStatement('        echo "i is bar"'));

$switchStatement2->CaseStatements->add($case5);

$case6 = new CaseStatement(new PrimitiveExpression('cake'));
$case6->Statements->add(new SnippetStatement('        "i is cake"'));

$switchStatement2->CaseStatements->add($case6);

/*
 * Generate the code and output the result.
 */

$provider->generateCodeFromStatement($switchStatement2, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

$writer->clear();

/*
 * Remove the break statements from the first switch statement
 */

foreach($switchStatement1->CaseStatements as $caseStatement)
{
    $caseStatement->hasBreak = false;
}

$provider->generateCodeFromStatement($switchStatement1, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

$writer->clear();

/*
 * Empty case statements
 */

$case7 = new CaseStatement(new PrimitiveExpression(3));
$case7->Statements->add(new SnippetStatement('        echo "i is 3"'));

$switchStatement1->CaseStatements->add($case7);

$switchStatement1->CaseStatements[0]->Statements->clear();
$switchStatement1->CaseStatements[1]->Statements->clear();
//$switchStatement1->CaseStatements[2]->Statements->clear();
$switchStatement1->CaseStatements[2]->Statements[0] = 
    new SnippetStatement('        echo "i is less than 3 but not negative"');
$switchStatement1->CaseStatements[2]->hasBreak = true;

$provider->generateCodeFromStatement($switchStatement1, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

$writer->clear();

/*
 * add default case statement
 */

$case8 = new CaseStatement(new PrimitiveExpression('default'));
$case8->Statements->add(new SnippetStatement('        echo "i is something else"'));
$case8->isDefault = true;

$switchStatement1->CaseStatements->add($case8);

$provider->generateCodeFromStatement($switchStatement1, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

/*
 * The output should be:
 * 
 * switch($i)
 * {
 *    case (0) :
 *        echo "i equals 0";
 *        break;
 *    case (1) :
 *        echo "i equals 1";
 *        break;
 *    case (2) :
 *        echo "i equals 2";
 *        break;
 * }
 *
 *
 * switch($i)
 * {
 *    case ('apple') :
 *        echo "i is apple";
 *        break;
 *    case ('bar') :
 *        echo "i is bar";
 *        break;
 *    case ('cake') :
 *        "i is cake";
 *        break;
 * }
 *
 *
 * switch($i)
 * {
 *    case (0) :
 *        echo "i equals 0";
 *    case (1) :
 *        echo "i equals 1";
 *    case (2) :
 *        echo "i equals 2";
 * }
 *
 *
 * switch($i)
 * {
 *    case (0) :
 *    case (1) :
 *    case (2) :
 *        echo "i is less than 3 but not negative";
 *        break;
 *    case (3) :
 *        echo "i is 3";
 *        break;
 * }
 *
 *
 * switch($i)
 * {
 *    case (0) :
 *    case (1) :
 *    case (2) :
 *        echo "i is less than 3 but not negative";
 *        break;
 *    case (3) :
 *        echo "i is 3";
 *        break;
 *    default : 
 *        echo "i is something else";
 * }
 * 
 */

?>
